/**
 * 
 */
package cubelabs.nexus.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.brickred.socialauth.Profile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cubelabs.nexus.dao.CategoryDAO;
import cubelabs.nexus.dao.BidDAO;
import cubelabs.nexus.dao.ChatMessageDAO;
import cubelabs.nexus.dao.CourseDAO;
import cubelabs.nexus.dao.EventDAO;
import cubelabs.nexus.dao.OpenTokArchiveDAO;
import cubelabs.nexus.dao.OpenTokSessionDAO;
import cubelabs.nexus.dao.SkillClassifiedDAO;
import cubelabs.nexus.dao.UserDAO;
import cubelabs.nexus.entity.Category;
import cubelabs.nexus.entity.Bid;
import cubelabs.nexus.entity.ChatMessage;
import cubelabs.nexus.entity.Course;
import cubelabs.nexus.entity.Event;
import cubelabs.nexus.entity.OpenTokArchive;
import cubelabs.nexus.entity.OpenTokSession;
import cubelabs.nexus.entity.SkillClassified;
import cubelabs.nexus.entity.User;
import cubelabs.nexus.entity.UserSubscription;

/**
 * @author deva
 *
 */
@Service
public class NexusService {

	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private OpenTokSessionDAO openTokSessionDAO;
	
	@Autowired
	private OpenTokArchiveDAO openTokArchiveDAO;
	
	@Autowired
	private EventDAO eventDAO;

	@Autowired
	private CategoryDAO categoryDAO;
	
	@Autowired
	private CourseDAO courseDAO;
	
	@Autowired
	private BidDAO bidDAO;
	
	@Autowired
	private SkillClassifiedDAO skillClassifiedDAO;
	
	@Autowired
	private ChatMessageDAO chatMessageDAO;

	@Transactional
	public User registerUser(Profile profile)  {
		
		System.out.println(profile.toString());
		
		User user = userDAO.findByEmail(profile.getEmail());
		
	
		
		if(user != null){
			user.lastLogin = new Date();
		}else{
			user = new User();
			user.email = profile.getEmail();
			user.name = profile.getFirstName()+ " " + profile.getLastName();
			user.active = true;
			user.emailValidated = true;
			user.lastLogin = new Date();
		
		}
		user = userDAO.makePersistent(user);
		return user;
	}
	
	@Transactional
	public void saveOrUpdateNexusSession(OpenTokSession openTokSession)  {
		openTokSessionDAO.makePersistent(openTokSession);
	}
	
	@Transactional
	public OpenTokSession findNexusSessionByEventId(Long eventId)  {
		return openTokSessionDAO.findByEventId(eventId);
	}
	
	@Transactional
	public Event findByEventId(Long eventId)  {
		return eventDAO.findById(eventId,false);
	}

	@Transactional
	public OpenTokSession findNexusSessionById(Long sessionId) {
		return openTokSessionDAO.findById(sessionId, false);
	}

	@Transactional
	public void saveOrUpdateNexusArchive(OpenTokArchive archive) {
		openTokArchiveDAO.makePersistent(archive);
	}

	@Transactional
	public OpenTokArchive findNexusArchiveByEventId(Long eventId) {
		return openTokArchiveDAO.findByEventId(eventId);
	}

	@Transactional
	public List<OpenTokArchive> findAllNexusArchiveByStatus(String status) {
		return openTokArchiveDAO.findAllByStatus(status);
	}

	@Transactional
	public OpenTokArchive findNexusArchiveById(String archiveId) {
		return openTokArchiveDAO.findByArchiveId(archiveId);
	}

	@Transactional
	public List<Category> findAllCategories() {
		return categoryDAO.findAll(); 
	}

	@Transactional
	public List<Course> findAllCourses() {
		return courseDAO.findAll();
	}

	@Transactional
	public List<Course> findAllCoursesByCategoryId(Long categoryId) {
		return courseDAO.findAllCoursesByCategoryId(categoryId);
	}

	@Transactional
	public void makeCoursePersistent(Course course){
		courseDAO.makePersistent(course);
		/*if(course.getId() == null){
			courseDAO.makePersistent(course);
		}else{
			courseDAO.merge(course);
		}*/
	}

	@Transactional
	public User findProfile(long userId) {
		return userDAO.findById(userId, false);
	}
	
	@Transactional
	public void addSkillClassified(SkillClassified skillClassified) {
		skillClassifiedDAO.makePersistent(skillClassified);
	}
	
	@Transactional
	public void addProposal(Bid bid) {
		bidDAO.makePersistent(bid);
	}

	@Transactional
	public List<Bid> findAllBidsByClassifiedId(long classifiedId){
		return bidDAO.findBidsByClassified(classifiedId);
	}

	@Transactional
	public List<SkillClassified> findAllClassifiedsByUser(long userId, boolean isGetBids) {
		List<SkillClassified> userClassifiedsList = skillClassifiedDAO.findAllClassifiedsByUser(userId);
		List<Bid> bidList = new ArrayList<Bid>();
		if(isGetBids){
			for(SkillClassified post:userClassifiedsList){
				bidList.addAll(bidDAO.findBidsByClassified(post));
			}
		}
		return userClassifiedsList;
	}
	
	@Transactional
	public List<SkillClassified> findAllClassifiedsByCategory(long categoryId, boolean isGetBids) {
		List<SkillClassified> categoryClassifiedsList = skillClassifiedDAO.findAllClassifiedsByCategory(categoryId);
		if(isGetBids){
			for(SkillClassified post:categoryClassifiedsList){
				List<Bid> bidList = new ArrayList<Bid>();
				bidList.addAll(bidDAO.findBidsByClassified(post));
				post.setBids(bidList);
			}
		}
		return categoryClassifiedsList;
	}
	
	@Transactional
	public List<Bid> findBidsByUser(long userId) {
		return bidDAO.findBidsByUser(userId);
	}
	
	@Transactional
	public SkillClassified findClassifiedById(long classifiedId, boolean isGetBids) {
		SkillClassified skillClassified = skillClassifiedDAO.findById(classifiedId, false);
		
		List<Bid> bidList = new ArrayList<Bid>();
		if(isGetBids){
			bidList.addAll(bidDAO.findBidsByClassified(skillClassified));
		}
		skillClassified.setBids(bidList);
		return skillClassified;
	}
	
	@Transactional
	public  Bid findBidById(long bidId) {
		return bidDAO.findById(bidId,false);
	}

	@Transactional
	public List<SkillClassified> findAllClassifieds(boolean isGetBids) {
		List<SkillClassified> skillClassifiedList = skillClassifiedDAO.findAll();
		if(isGetBids){
			for(SkillClassified skillClassified:skillClassifiedList){
			}
		}
		return skillClassifiedList;
	}
	

	@Transactional
	public List<ChatMessage> fetchMessagesByBid(Long bidId) {
		return chatMessageDAO.findByBid(bidId);
	}
}
